﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Song.cs" company="Jens Horstmann">
//   Copyright (c) Jens Horstmann. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace MiniMusicPlayer.Classes
{
    using System.ComponentModel;
    using System.IO;

    using File = TagLib.File;

    /// <summary>
    /// The song.
    /// </summary>
    public class Song : INotifyPropertyChanged
    {
        /// <summary>
        /// The file info.
        /// </summary>
        private FileInfo fileInfo;


        /// <summary>
        /// Initializes a new instance of the <see cref="Song"/> class.
        /// </summary>
        /// <param name="fileInfo">
        /// The file info.
        /// </param>
        public Song(FileInfo fileInfo)
        {
            this.FileInfo = fileInfo;
            this.Format = this.FileInfo.Extension;

            this.tagLibFile = File.Create(this.FileInfo.FullName);
            this.Album = this.tagLibFile.Tag.Album;
            this.Title = this.tagLibFile.Tag.Title;
            this.AlbumArtists = HelperClass.StringArrayToString(this.tagLibFile.Tag.AlbumArtists);
            this.Performers = HelperClass.StringArrayToString(this.tagLibFile.Tag.Performers);            
        }

        /// <summary>
        /// Gets or sets the tag lib file.
        /// </summary>
        private File tagLibFile { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the album.
        /// </summary>
        public string Album { get; set; }

        /// <summary>
        /// Gets or sets the album artists.
        /// </summary>
        public string AlbumArtists { get; set; }

        /// <summary>
        /// Gets or sets the performers.
        /// </summary>
        public string Performers { get; set; }

        /// <summary>
        /// Gets or sets the format.
        /// </summary>
        public string Format { get; set; }

        /// <summary>
        /// Gets or sets the file info.
        /// </summary>
        public FileInfo FileInfo
        {
            get
            {
                return this.fileInfo;
            }

            set
            {
                this.fileInfo = value;
                this.OnPropertyChanged("FileInfo");
            }
        }

        /// <summary>
        /// The property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// The on property changed.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        protected void OnPropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public override string ToString()
        {
            return this.Title + " - " + this.Performers;
        }
    }
}